package handler

import (
	"car-trade-mgr/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func CarAdd(c *gin.Context) {
	carName := c.PostForm("carName")
	carContent := c.PostForm("carContent")
	carPrice, _ := strconv.Atoi(c.PostForm("carPrice"))

	car := model.Car{
		CarName:    carName,
		CarContent: carContent,
		CarPrice:   carPrice,
	}

	err := model.DB.Create(&car).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	orderName := c.PostForm("orderName")
	orderContent := c.PostForm("orderContent")
	orderPrice, _ := strconv.Atoi(c.PostForm("orderPrice"))
	pid, _ := strconv.Atoi(c.PostForm("pid"))

	order := model.Order{
		OrderName:    orderName,
		OrderContent: orderContent,
		OrderPrice:   orderPrice,
		Pid:          pid,
	}

	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
